<template>
	<view class="container"
		style="padding-top: 88upx;background: url('https://qiniu.dadaozhixing.top/dxx/pages/user/page_user_header.png') no-repeat;background-size: 750upx 472upx;">



		<view class="user-section">
			<view class="user-info-box">
				<view class="portrait-box" @click="navTo('/pages/set/set')">
					<image class="portrait"
						:src="userInfo.portrait || 'https://qiniu.dadaozhixing.top/dxx/pages/user/default_header.png'">
					</image>
				</view>
				<view class="info-box" v-if="userInfo.id">
					<text class="username">{{userInfo.name}}</text>
				</view>
				<view class="login-container" v-else>
					<text class="login" @click="navToLogin">登录/注册</text>
					<text class="welcome">Hi 欢迎来到冻小鲜</text>
				</view>
			</view>


		</view>
		<view class="order-section">
			<view class="order-item" v-for="(menu, mIdx) in orderMenuList" :key="mIdx"
			  @click="navTo(menu.path)">
				<image :src="menu.icon"></image>
				<text>{{ menu.name }}</text>
			</view>
		</view>


		<view class="cover-container" :style="[{
				transform: coverTransform,
				transition: coverTransition
			}]" @click="goToMyGiftCard">

			<image mode="aspectFill" src="https://qiniu.dadaozhixing.top/dxx/pages/user/advs.png"
				style="width: 702upx;height: 160upx;"></image>
		</view>
		
		<view class="my-jump-list">
			<div class="header">常用工具</div>
			<uni-list :border="false">
				<uni-list-item v-for="(jump,jIdx) in jumpList" :key="jIdx" showArrow 
				:title="jump.name" :to="jump.path"
				 @click="jumpRet"/>
			</uni-list>
		</view>

	</view>
</template>
<script>
	import {
		mapState
	} from 'vuex';
	let startY = 0,
		moveY = 0,
		pageAtTop = true;
	export default {
		components: {},
		data() {
			return {
				coverTransform: 'translateY(0px)',
				coverTransition: '0s',
				moving: false,
				orderMenuList: [{
						name: '待付款',
						icon: 'https://qiniu.dadaozhixing.top/dxx/pages/user/paying.png',
						path: "/pages/order/order?state=1"
					},
					{
						name: '待收货',
						icon: 'https://qiniu.dadaozhixing.top/dxx/pages/user/delivering.png',
						path: "/pages/order/order?state=2"
					},
					{
						name: '已完成',
						icon: 'https://qiniu.dadaozhixing.top/dxx/pages/user/completed.png',
						path: "/pages/order/order?state=0"
					},
					{
						name: '全部订单',
						icon: 'https://qiniu.dadaozhixing.top/dxx/pages/user/all_orders.png',
						path: "/pages/order/order?state=0"
					},
				],
				jumpList: [
					{name: "我的收藏", path: "/pages/shoucang/shoucang"},
					{name: "收货地址", path: "/pages/address/address"},
					{name: "商务合作", path: "/pages/user/businessCooperation"},
					{name: "意见反馈", path: "/pages/user/suggestion"},
					{name: "我的客服", path: ""},
					{name: "关于我们", path: ""}
				]
			}
		},
		onLoad() {},
		// #ifndef MP
		onNavigationBarButtonTap(e) {
			const index = e.index;
			if (index === 0) {
				this.navTo('/pages/set/set');
			} else if (index === 1) {
				// #ifdef APP-PLUS
				const pages = getCurrentPages();
				const page = pages[pages.length - 1];
				const currentWebview = page.$getAppWebview();
				currentWebview.hideTitleNViewButtonRedDot({
					index
				});
				// #endif
				uni.navigateTo({
					url: '/pages/notice/notice'
				})
			}
		},
		// #endif
		computed: {
			...mapState(['hasLogin', 'userInfo'])
		},
		methods: {

			/**
			 * 统一跳转接口,拦截未登录路由
			 * navigator标签现在默认没有转场动画，所以用view
			 */
			navTo(url) {
				if (!this.hasLogin) {
					url = '/pages/public/login';
				}
				uni.navigateTo({
					url
				})
			},

			/**
			 *  会员卡下拉和回弹
			 *  1.关闭bounce避免ios端下拉冲突
			 *  2.由于touchmove事件的缺陷（以前做小程序就遇到，比如20跳到40，h5反而好很多），下拉的时候会有掉帧的感觉
			 *    transition设置0.1秒延迟，让css来过渡这段空窗期
			 *  3.回弹效果可修改曲线值来调整效果，推荐一个好用的bezier生成工具 http://cubic-bezier.com/
			 */
			coverTouchstart(e) {
				if (pageAtTop === false) {
					return;
				}
				this.coverTransition = 'transform .1s linear';
				startY = e.touches[0].clientY;
			},
			coverTouchmove(e) {
				moveY = e.touches[0].clientY;
				let moveDistance = moveY - startY;
				if (moveDistance < 0) {
					this.moving = false;
					return;
				}
				this.moving = true;
				if (moveDistance >= 80 && moveDistance < 100) {
					moveDistance = 80;
				}

				if (moveDistance > 0 && moveDistance <= 80) {
					this.coverTransform = `translateY(${moveDistance}px)`;
				}
			},
			coverTouchend() {
				if (this.moving === false) {
					return;
				}
				this.moving = false;
				this.coverTransition = 'transform 0.3s cubic-bezier(.21,1.93,.53,.64)';
				this.coverTransform = 'translateY(0px)';
			},
			navToLogin(){
				uni.navigateTo({
					url: '/pages/public/login'
				})
			},
			jumpRet(ret){
				console.log(ret)
			},
			goToMyGiftCard () {
				uni.navigateTo({
					url: '/pages/giftcards/myGiftCard'
				})
			}
		}
	}
</script>
<style lang='scss' scoped>
	%flex-center {
		display: flex;
		flex-direction: column;
		justify-content: center;
		align-items: center;
	}

	%section {
		display: flex;
		justify-content: space-around;
		align-content: center;
		background: #fff;
		border-radius: 10upx;
	}

	.container {
		padding: 0 30upx;
	}

	.user-section {
		position: relative;

		.bg {
			position: absolute;
			left: 0;
			top: 0;
			width: 100%;
			height: 100%;
			filter: blur(1px);
			opacity: .7;
		}
	}

	.user-info-box {
		height: 180upx;
		display: flex;
		align-items: center;
		position: relative;
		z-index: 1;

		.portrait {
			width: 130upx;
			height: 130upx;
			border: 5upx solid #fff;
			border-radius: 50%;
		}

		.username {
			font-size: $font-lg + 6upx;
			color: $font-color-dark;
			margin-left: 20upx;
		}
	}

	.login-container {
		display: flex;
		flex-direction: column;

		.login {
			font-family: PingFangSC, PingFang SC;
			font-weight: 500;
			font-size: 32rpx;
			color: #FFFFFF;
			line-height: 48rpx;
			text-align: left;
			font-style: normal;
		}

		.welcome {
			font-family: PingFangSC, PingFang SC;
			font-weight: 400;
			font-size: 24rpx;
			color: #FFFFFF;
			line-height: 36rpx;
			text-align: left;
			font-style: normal;
		}
	}

	.order-section {
		width: 702rpx;
		height: 180rpx;
		background: #FFFFFF;
		border-radius: 16rpx;
		display: flex;
		align-items: center;
		justify-content: space-around;

		.order-item {
			display: flex;
			flex-direction: column;
			align-items: center;

			image {
				width: 64upx;
				height: 64upx;
			}
		}
	}

	.cover-container {
		margin-top: 24upx;
	}
	
	.my-jump-list {
		margin-top: 24upx;
		border-radius: 16upx;
		padding: 24upx;
		.header{
			font-weight: bolder;
			font-size: 32rpx;
			color: #23242E;
			line-height: 48rpx;
			text-align: left;
			font-style: normal;
		}
		::v-deep .uni-list-item__container{
			padding-left: 0;
		}
	}
	
</style>